//
//  expressViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/14.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "expressViewController.h"

static NSString *const Rcell = @"Rcell";

@interface expressViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *myTable;
@property(nonatomic, retain)NSMutableArray *myArray;
@property(nonatomic, retain)MBProgressHUD *HUB;
@property(nonatomic, retain)NSMutableArray *images;

@end

@implementation expressViewController

-(void)dealloc
{
    [_myTable release];
    [_myArray release];
    [_images release];
    [_HUB release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.myArray = [NSMutableArray array];
    self.images = [NSMutableArray array];
    
    [self creatTable];
    
    //    self.HUB = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //    self.HUB.mode = MBProgressHUDModeIndeterminate;//不同类型
    //    [self.HUB show:YES];
    //    self.HUB.labelText = @"loading...";
    //    self.isUpLoading = NO;
    
    //上下拉图片数组
    self.images = [NSMutableArray array];
    for (int i = 1; i < 4; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading_0%d.png",i]];
        [self.images addObject:image];
    }
    
    [self addHeader];
    [self addFooter];
    [self.myTable.mj_header beginRefreshing];
    
    // Do any additional setup after loading the view.
    
    
}
//下拉刷新
-(void)addHeader{
    
    __block expressViewController *vc = self;
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        vc.isUpLoading = NO;
        [vc getData:@"http://app.api.autohome.com.cn/autov5.0.0/news/fastnewslist-pm4-b0-l0-s20-lastid0.json"];
        
    }];
    
    [header setImages:self.images forState:MJRefreshStateIdle];
    [header setImages:self.images forState:MJRefreshStatePulling];
    [header setImages:self.images forState:MJRefreshStateRefreshing];
    self.myTable.mj_header = header;
    
    
}
//上拉刷新
-(void)addFooter{
    __block expressViewController *vc = self;
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        // vc.nextPage ++;
        vc.isUpLoading = YES;
        expressMo *model = [self.myArray lastObject];
        [vc getData:[NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov5.0.0/news/fastnewslist-pm4-b0-l0-s20-lastid%@.json",model.lastid]];
        
    }];
    
    [footer setImages:self.images forState:MJRefreshStateRefreshing];
    self.myTable.mj_footer = footer;
    
}


-(void)creatTable{
    self.myTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-157) style:UITableViewStylePlain];
    self.myTable.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTable];
    self.myTable.delegate = self;
    self.myTable.dataSource = self;
    [self.myTable registerClass:[expressCell class] forCellReuseIdentifier:Rcell];
    self.myTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.myTable release];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    expressCell *cell = [tableView dequeueReusableCellWithIdentifier:Rcell forIndexPath:indexPath];
    cell.model = [self.myArray objectAtIndex:indexPath.row];
    cell.selectionStyle = NO;
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.myArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.view.frame.size.height/3*1.5;
}

//跳转详情页面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    expressDataViewController *exPvc = [[expressDataViewController alloc] init];
    expressMo *model = [self.myArray objectAtIndex:indexPath.row];
    exPvc.model = model;
    
    [self presentViewController:exPvc animated:YES completion:^{
        
        
    }];
    
    [exPvc release];
    
}

-(void)getData:(NSString *)str{
    
    [ZYNetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        
        NSMutableDictionary *result = [responseObject objectForKey:@"result"];
        NSMutableArray *list = [result objectForKey:@"list"];
        for (NSDictionary *dic in list) {
            expressMo *model = [[expressMo alloc] init];
            [model yy_modelSetWithDictionary:dic];
            [self.myArray addObject:model];
            [model release];
            
        }
        [self.myTable.mj_header endRefreshing];
        [self.myTable.mj_footer endRefreshing];
        
        [self.myTable reloadData];
        
    } filed:^(NSError *error) {
        
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
